package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.IPowerDnsService;
import com.macasa.panel.entity.api.IPowerDnsServiceDomain;

@Entity
@Table(name = "power_dns_service")
@Inheritance(strategy = InheritanceType.JOINED)
@Getter
@Setter
@ToString(callSuper = true)
public abstract class PowerDnsService extends Service implements IPowerDnsService {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long				serialVersionUID	= -1334376039634550864L;

	/**
	 * Nomn de l'entity xml qui gere la table domains sur le serveur sql hebergeant
	 */
	@Column(name = "domainsEntity", nullable = false)
	private String							domainsBackendEntity;

	/**
	 * Nomn de l'entity xml qui gere la table records sur le serveur sql hebergeant
	 */
	@Column(name = "recordsEntity", nullable = true)
	private String							recordsBackendEntity;

	@OneToMany(mappedBy = "powerDnsService", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.PowerDnsServiceDomain.class)
	@ForeignKey(name = "fk_power_dns_service_domain_01")
	private List<IPowerDnsServiceDomain>	powerDnsServicedomains;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PowerDnsService)) {
			return false;
		}

		final PowerDnsService other = CastHelpers.cast(obj, PowerDnsService.class);

		return super.equals(obj) && Objects.equal(this.domainsBackendEntity, other.domainsBackendEntity)
				&& Objects.equal(this.recordsBackendEntity, other.recordsBackendEntity)
				&& Objects.equal(this.powerDnsServicedomains, other.powerDnsServicedomains);
	}

	@Override
	public int hashCode() {
		return super.hashCode() + Objects.hashCode(this.domainsBackendEntity, this.recordsBackendEntity, this.powerDnsServicedomains);
	}
}
